import 'dart:async';

import 'package:convergence_media/features/user/service/main_auth_service.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'login_controller.g.dart';

enum LoginState {
  init,
  waitingCode,
  submiting,
}

@riverpod
class LoginController extends _$LoginController {
  @override
  FutureOr<LoginState> build() => LoginState.init;

  Future<void> sendSms(String phone) async {
    final mainAuthService = ref.read(mainAuthServiceProvider);

    state = const AsyncLoading();
    state = await AsyncValue.guard(() async {
      await mainAuthService.getVerificationCode(phone);
      return LoginState.waitingCode;
    });
  }

  Future<void> login(String phone, String verificationCode) async {
    final mainAuthService = ref.watch(mainAuthServiceProvider);

    state = const AsyncLoading();
    state = await AsyncValue.guard(() async {
      await mainAuthService.login(phone, verificationCode);
      return LoginState.submiting;
    });
  }
}
